let fs = require('fs')
let path = require('path')
let http = require('http')
let url = require('url')

let aa = JSON.parse( fs.readFileSync('data.json', 'UTF-8'));
http.createServer((req, res) => {
    if (req.url === '/favicon.ico') return;
    let pathname = url.parse(req.url).pathname;
    if (pathname === '/') {
        res.end(fs.readFileSync(path.join(__dirname, 'index.html')))
    }
    if (path.extname(pathname)) {
        res.end(fs.readFileSync(path.join(__dirname, req.url)))
    }
    if (pathname === '/log') {
        let data = ''
        req.on('data', chunk => {
            data += chunk
        })
        req.on('end', () => {
            data = JSON.parse(data)
            let result=aa.filter(item=>{
                if(item.name===data.inp){
                    return item;
                }
            });
            if(result.length>1){
                res.end('用户名不对请从新输入')
            }
            if(result.length<0){
                if(result[0].pwd===data.pwd){
                    res.end('密码正确')
                }else{
                    res.end('密码错误')
                }
            }   
            
            res.end()
        })
    }

    
}).listen(5000)
